package com.qsl.ssyx.common.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import lombok.Data;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

// Redis的Redisson多线程-读取ymlwe文件数据
@Data
@Configuration // 配置类
@ConfigurationProperties(prefix = "spring.redis") // 实体类方式-读取ymlwe文件数据
public class RedissonConfig {

    private String host;

    private String addresses;

    private String password;

    private String port;

    private int timeout = 3000;
    private int connectionPoolSize = 64;
    private int connectionMinimumIdleSize = 10;
    private int pingConnectionInterval = 60000;
    private static String ADDRESS_PREFIX = "redis://";

    /**
     * 自动配置
     */
    @Bean
    RedissonClient redissonClient() {
        Config config = new Config();
        //  判断redis 的host是否为空
        if (StringUtils.isEmpty(host)) {
            throw new RuntimeException("host is  empty");
        }
        // 配置host,port等参数
        SingleServerConfig serverConfig = config.useSingleServer()
                //redis://192.168.139.140:7181
                .setAddress(ADDRESS_PREFIX + this.host + ":" + port)
                .setTimeout(this.timeout)
                .setPingConnectionInterval(pingConnectionInterval)
                .setConnectionMinimumIdleSize(this.connectionMinimumIdleSize);
        // 判断redis，是否设置密码
        if (!StringUtils.isEmpty(this.password)) {
            serverConfig.setPassword(this.password);
        }
        RedissonClient redisson = Redisson.create(config);
        return Redisson.create(config);
    }
}
